package cn.sciento.farm.devicemanager.gateway.model.downlink;

import cn.sciento.farm.devicemanager.common.constant.DeviceConstant;
import cn.sciento.farm.devicemanager.common.constant.LoraWanConstant;
import cn.sciento.farm.devicemanager.gateway.model.lorawan.DeviceTx;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Base64;

/**
 * @author wumu
 * @version V1.0
 * @date 9/21/18
 * @time 4:47 PM
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class LoraWanControlData extends BaseBlockControlData {



    public LoraWanControlData() {
        this.topicPrefix = "";
        this.protocol = DeviceConstant.LORAWAN;
    }

    @Override
    public MqttContent generate() {
        MqttContent mqttContent = new MqttContent();
        String topic = String.format(LoraWanConstant.DOWNLINK_TOPIC_TEMPLATE,
                LoraWanConstant.APPLICATION_ID,
                concentrator
                );
        DeviceTx deviceTx = new DeviceTx();
        deviceTx.setDevEUI(concentrator);
        String data = Base64.getEncoder().encodeToString(generateData());
        deviceTx.setData(data);
        deviceTx.setConfirmed(true);
        mqttContent.setTopic(topic);
        mqttContent.setPayload(JSON.toJSONString(deviceTx));
        return mqttContent;
    }

    private byte[] generateData(){
        if(duration >0){
            byte[] bytes = new byte[4];
            bytes[0] = (byte)( Integer.parseInt(circuit)*16 + 1);
            bytes[1] = (byte)en;
            bytes[2] = (byte)(duration/256);
            bytes[3] = (byte)(duration%256);
            return bytes;
        }else {
            byte[] bytes = new byte[2];
            bytes[0] = (byte)(Integer.parseInt(circuit) * 16);
            bytes[1] = (byte)en;
            return bytes;
        }

    }

    public static LorawanControlMqttDataBuilder builder() {
        return new LorawanControlMqttDataBuilder();
    }


    public static final class LorawanControlMqttDataBuilder {
        String concentrator;
        String node;
        String circuit;
        int duration;
        int en;
        int delay;

        private LorawanControlMqttDataBuilder() {
        }


        public LorawanControlMqttDataBuilder concentrator(String concentrator) {
            this.concentrator = concentrator;
            return this;
        }

        public LorawanControlMqttDataBuilder node(String node) {
            this.node = node;
            return this;
        }

        public LorawanControlMqttDataBuilder circuit(String circuit) {
            this.circuit = circuit;
            return this;
        }

        public LorawanControlMqttDataBuilder duration(int duration) {
            this.duration = duration;
            return this;
        }

        public LorawanControlMqttDataBuilder en(int en) {
            this.en = en;
            return this;
        }

        public LorawanControlMqttDataBuilder delay(int delay) {
            this.delay = delay;
            return this;
        }

        public LoraWanControlData build() {
            LoraWanControlData lorawanControlData = new LoraWanControlData();
            lorawanControlData.setConcentrator(concentrator);
            lorawanControlData.setNode(node);
            lorawanControlData.setCircuit(circuit);
            lorawanControlData.setDuration(duration);
            lorawanControlData.setEn(en);
            lorawanControlData.setDelay(delay);
            return lorawanControlData;
        }
    }
}
